package com.example.pceb.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pceb.VO.ResumeSendVO;
import com.example.pceb.entity.Resume;
import com.example.pceb.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
@Mapper
public interface ResumeMapper extends BaseMapper<Resume> {
    @Results({
            @Result(property = "studentName", column = "student_name"),
            @Result(property = "studentPhone", column = "student_phone"),
            @Result(property = "studentEmail", column = "student_email"),
            @Result(property = "recruitName", column = "recruit_name"),
            @Result(property = "enterprise", column = "enterprise")
    })
    @SelectProvider(type = ResumeSqlBuilder.class, method = "buildReceivedResumesSql")
    List<ResumeSendVO> findReceivedResumes(@Param("enterprise") String enterprise,
                                           @Param("studentName") String studentName,
                                           @Param("recruitName") String recruitName,
                                           @Param("page") int page,
                                           @Param("size") int size);

    @SelectProvider(type = ResumeSqlBuilder.class, method = "buildReceivedResumesCountSql")
    int countReceivedResumes(@Param("enterprise") String enterprise,
                             @Param("studentName") String studentName,
                             @Param("recruitName") String recruitName);
}
// SQL 构建器类
class ResumeSqlBuilder {
    public String buildReceivedResumesSql(Map<String, Object> params) {
        return new SQL(){{
            SELECT("rs.*, resume.name AS student_name, resume.phone AS student_phone, "
                    + "resume.email AS student_email, r.identity AS recruit_name, r.enterprise");
            FROM("resume_send rs");
            JOIN("recruit r ON rs.recruit_id = r.id");
            JOIN("resume resume ON rs.username = resume.username");
            WHERE("r.enterprise = #{enterprise}");
            if (params.get("studentName") != null) {
                WHERE("resume.name LIKE CONCAT('%', #{studentName}, '%')");
            }
            if (params.get("recruitName") != null) {
                WHERE("r.identity LIKE CONCAT('%', #{recruitName}, '%')");
            }
            ORDER_BY("rs.send_time DESC");
            LIMIT("#{size} OFFSET #{offset}");
        }}.toString();
    }

    public String buildReceivedResumesCountSql(Map<String, Object> params) {
        return new SQL(){{
            SELECT("COUNT(*)");
            FROM("resume_send rs");
            JOIN("recruit r ON rs.recruit_id = r.id");
            WHERE("r.enterprise = #{enterprise}");
            if (params.get("studentName") != null) {
                WHERE("EXISTS (SELECT 1 FROM resume WHERE username = rs.username AND name LIKE CONCAT('%', #{studentName}, '%'))");
            }
            if (params.get("recruitName") != null) {
                WHERE("r.identity LIKE CONCAT('%', #{recruitName}, '%')");
            }
        }}.toString();
    }
}
